<?php

namespace app\index\controller;
use think\Controller;
use think\Db;
use think\Request;

class Role extends Common {
	public function _initialize() {
		parent::_initialize();
	}
	public function index() {
		$this->getAuth();

		$data = Db::table('cn_character')
			->alias('cc')
			->join('cn_priv_character_relation_new cpcr', 'cpcr.chara_id  = cc.chara_id')
			->join('cn_priv_new cp', 'cp.priv_id = cpcr.priv_id')
			->field('cc.chara_name,cc.chara_id,cp.priv_name')
			->where('cc.is_delete', 'eq', 0)
			->where('cp.is_delete', 'eq', 0)
		// ->where('cp.is_delete','neq','2')
			->order('cc.chara_id')
		// ->paginate(5);
			->select();
		// dump($data);die;
		$res = Db::name('character')
			->field('chara_id,chara_name')
			->where('is_delete', 'eq', 0)
			->where('chara_id', 'neq', 0)
			->order('sort', 'ASC')
		// ->where('chara_id','neq','3')
			->paginate(10);
		$page = $res->render();
		// var_dump($data);
		// dump($res);die;
		return view('index@Role/index', [
			'data' => $data,
			'res' => $res,
			'page' => $page,
		]);
	}

	public function add() {
		// dump(123);die;
		return view('index@Role/add');
	}
	// 添加角色
	public function insert(Request $request) {
		$uid = $_SESSION['think']['username'][0]['user_id'];
		$time = date('Y-m-d H:i:s', time());
		$data = $request->post();
		$d['Name'] = $data['Name'];
		$d['Remark'] = $data['Remark'];
		if (empty($d['Name'])) {
			$this->error('角色名为空', 'index/Role/index');
		}
		$s = [
			'chara_name' => $d['Name'],
			'chara_desc' => $d['Remark'],
			'create_user_id' => $uid,
			'create_time' => $time,
			'update_user_id' => $uid,
			'update_time' => $time,
			'is_delete' => 0,
			'sort' => 50,

		];
		// dump($s);die;
		$result = Db('character')->insertGetId($s);
		// dump($result);die;

		$d = [
			'priv_id' => '3', //默认3的权限
			'chara_id' => $result,
			'create_user_id' => $uid,
			'create_time' => $time,
			'update_user_id' => $uid,
			'update_time' => $time,
			'is_delete' => 0,
		];
		$list = db('priv_character_relation')->insert($d);

		if ($list) {
			return $this->success('新增成功', 'index/Role/index');
		} else {
			return $this->error('新增失败', 'index/Role/index');
		}
	}

	public function delete() {
		// echo $id;
		// dump($_POST);die;
		$id = $_POST['chara_id'];
		$d = [
			'is_delete' => '1',
		];
		$result = db('character')
			->where('chara_id', $id)
			->update($d);
		// dump($result);die;
		//还要删除
		return $result;
	}

	// 修改角色
	public function edit($id) {
		$data = db('character')->find($id);
		// dump($data);die;
		return view('index@Role/edit', [
			'title' => '我是修改ID:' . $id . '角色页面',
			'data' => $data,
			'id' => $id,
		]);
	}
	//修改权限 主要是排序
	public function role_edit($id) {
		$data = db('priv_new')->find($id);
		return view('index@Role/role_edit', [
			'title' => '我是修改ID:' . $id . '权限页面',
			'data' => $data,
			'id' => $id,
		]);
	}

	public function update(Request $request) {
		$data = $request->post();
		// var_dump($data);die;
		$id = $data['id'];
		$a = [
			'chara_name' => $data['Name'],
			'sort' => $data['sort'],
		];

		$result = Db::name('character')->where('chara_id', $id)->update($a);
		if ($result) {
			return $this->success('编辑成功', 'index/Role/index');
		} else {
			return $this->error('编辑失败', 'index/Role/index');
		}
	}

	public function role_update(Request $request) {
		$data = $request->post();
		// var_dump($data);die;
		$id = $data['id'];
		$a = [
			'priv_name' => $data['Name'],
			'sort' => $data['sort'],
		];

		$result = Db::name('priv_new')->where('priv_id', $id)->update($a);
		if ($result) {
			return $this->success('编辑成功', 'index/Role/index');
		} else {
			return $this->error('编辑失败', 'index/Role/index');
		}
	}

	// 分配角色
	public function getPower($id) {
		$input = input();
		$rid = $input['id'];
		// dump($rid);die;
		$list = Db::table('cn_character')
			->alias('cc')
			->join('cn_priv_character_relation_new cpcr', 'cpcr.chara_id  = cc.chara_id')
			->join('cn_priv_new cp', 'cp.priv_id = cpcr.priv_id')
			->field('cp.priv_name as sapname,cp.priv_id as sapid')
			->where('cc.chara_id', $rid)
		// ->where('cp.is_delete','neq','2')
			->select();
		// dump($list);die;
		// $rname = $list[0];
		$role = Db::name('priv_new')
			->field(['priv_id', 'priv_name,sort'])
			->order('sort', 'ASC')
		// ->where('is_delete','neq','2')
			->select();
		foreach ($list as $v) {
			$myrole[] = $v['sapid'];
		}
//		foreach ($role as $key => $value) {
//			$role[$key]['top'] = '';
//		}
//		for ($y = 0; $y < count($role); $y++) {
//			$role[$y]['top'] = substr($role[$y]['priv_name'], 0, strrpos($role[$y]['priv_name'], "-"));
//		}
//		dump($role);die;
		// foreach ($role as $key => $value) {
		// 	$role[$key] = $role[$key]['top'];
		// }
		// dump($role);die;
		if (isset($myrole)) {
			return view('index@Role/choosePower', [
				'role' => $role,
				'list' => $myrole,
				'rid' => $rid,
			]);
		} else {
			return view('index@Role/choosePower', [
				'role' => $role,
				'rid' => $rid,
			]);
		}

	}

	public function updatePower(Request $request) {
		$data = $request->post();
		// dump($data);die;
		$id = $data['rid'];
		// dump($id);die;
		// 删除原来节点
		$r = Db::name('priv_character_relation_new')
			->where('chara_id', $id)
			->delete();
		// var_dump($r);die;

		foreach ($data['power'] as $k => $v) {
			$a = [
				'priv_id' => $v,
				'chara_id' => $id,
			];
			$result = Db::name('priv_character_relation_new')
				->where('chara_id', $id)
				->insert($a);
		}

		// dump($result);die;

		if ($result) {
			return $this->success('分配节点成功', 'index/Role/index');
		} else {
			return $this->error('分配节点失败', 'index/Role/index');
		}
	}

}
